<template>
  <div class="seller-list-container">
    <div class="search-form">
      <el-form :inline="true" :model="searchForm" ref="searchFormRef">
        <el-form-item label="卖家名称" prop="name">
          <el-input v-model="searchForm.name" placeholder="请输入卖家名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table :data="tableData" border style="width: 100%" v-loading="loading">
      <el-table-column type="index" label="序号" width="60" align="center"></el-table-column>
      <el-table-column prop="username" label="用户名" min-width="120"></el-table-column>
      <el-table-column prop="name" label="姓名" min-width="120"></el-table-column>
      <el-table-column prop="phone" label="手机号" min-width="120"></el-table-column>
      <el-table-column prop="totalSales" label="累计成交数" min-width="120"></el-table-column>
      <el-table-column prop="approvalDate" label="认证通过日期" min-width="180"></el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="searchForm.page" :page-sizes="[10, 20, 50, 100]" :page-size="searchForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, computed } from 'vue'
import { getSellerList } from '@/api/seller'
import { ElMessage } from 'element-plus'

// 搜索表单
const searchForm = reactive({
  name: '',
  page: 1,
  pageSize: 10
})

const searchFormRef = ref(null)
const loading = ref(false)
const tableData = ref([])
const total = ref(0)

// 加载卖家列表
const loadSellerList = async () => {
  loading.value = true
  try {
    const res = await getSellerList(searchForm)
    console.log('获取到的卖家列表数据:', res)

    if (res && res.records && res.records.length > 0) {
      tableData.value = res.records
      total.value = res.total
    } else if (res && Array.isArray(res) && res.length > 0) {
      tableData.value = res
      total.value = res.length
    } else {
      tableData.value = []
      total.value = 0
      console.error('返回的数据格式不符合预期或数据为空:', res)
    }
  } catch (error) {
    console.error('加载卖家列表失败:', error)
    ElMessage.error('加载卖家列表失败')
    tableData.value = []
    total.value = 0
  } finally {
    loading.value = false
  }
}

// 查看卖家详情
// const handleDetail = (row) => {
//   console.log('查看卖家详情:', row)
//   // 实现卖家详情查看逻辑，可以使用弹窗或者跳转到详情页面
//   ElMessage.info('查看卖家详情功能暂未实现')
// }

// 搜索
const handleSearch = () => {
  searchForm.page = 1
  loadSellerList()
}

// 重置搜索
const resetSearch = () => {
  searchFormRef.value.resetFields()
  searchForm.page = 1
  loadSellerList()
}

// 分页处理
const handleSizeChange = (val) => {
  searchForm.pageSize = val
  loadSellerList()
}

const handleCurrentChange = (val) => {
  searchForm.page = val
  loadSellerList()
}

// 页面加载时获取数据
onMounted(() => {
  loadSellerList()
})
</script>

<style scoped>
.seller-list-container {
  padding: 20px;
}

.search-form {
  margin-bottom: 20px;
  background-color: #fff;
  padding: 20px;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
}
</style>